package com.qymerge.manage.core.third.wechat.service.material;

import com.qymerge.manage.core.third.wechat.config.WechatCommonConfig;
import com.qymerge.manage.core.third.wechat.dto.material.MaterialDTO;
import com.qymerge.manage.core.third.wechat.dto.material.MaterialNewsItemDTO;
import com.qymerge.manage.core.third.wechat.dto.material.MaterialRequestDTO;
import com.qymerge.manage.core.third.wechat.dto.material.MaterialVideoItemDTO;
import com.qymerge.manage.core.utils.HttpRequestUtil;
import com.qymerge.manage.core.utils.WxGsonUtil;
import com.google.gson.reflect.TypeToken;

import java.text.MessageFormat;

/**
 * Desc 素材相关
 * User cong.li
 * Date 2017/6/7
 * Time 20:42
 */
public class WechatMaterialService {

    /**
     * 获取素材列表
     *
     * @param accessToken
     * @param materialRequestDTO
     * @return
     */
    public static MaterialDTO batchgetMaterial(String accessToken, MaterialRequestDTO materialRequestDTO) {
        String url = MessageFormat.format(WechatCommonConfig.url_batchget_material, accessToken);
        String result = HttpRequestUtil.requestPostBody(url, WxGsonUtil.toJson(materialRequestDTO));
        MaterialDTO materialDto = WxGsonUtil.json2T(result, new TypeToken<MaterialDTO>() {
        }.getType());
        return materialDto;
    }

    /**
     * 获取图文素材 根据素材ID
     *
     * @param accessToken
     * @param materialId
     * @return
     */
    public static MaterialNewsItemDTO getNewsMaterial(String accessToken, String materialId) {
        String url = MessageFormat.format(WechatCommonConfig.url_get_material, accessToken);
        String result = HttpRequestUtil.requestPostBody(url, "{\"media_id\":\"" + materialId + "\"}");
        MaterialNewsItemDTO materialNewsItemDto = WxGsonUtil.json2T(result, new TypeToken<MaterialNewsItemDTO>() {
        }.getType());
        return materialNewsItemDto;
    }

    /**
     * 获取视频素材 根据素材ID
     *
     * @param accessToken
     * @param materialId
     * @return
     */
    public static MaterialVideoItemDTO getVideoMaterial(String accessToken, String materialId) {
        String url = MessageFormat.format(WechatCommonConfig.url_get_material, accessToken);
        String result = HttpRequestUtil.requestPostBody(url, "{\"media_id\":\"" + materialId + "\"}");
        MaterialVideoItemDTO materialNewsItemDto = WxGsonUtil.json2T(result, new TypeToken<MaterialVideoItemDTO>() {
        }.getType());
        return materialNewsItemDto;
    }
}
